package mylib.dir;

/**
 * Created with IntelliJ IDEA.
 * User: 1
 * Date: 10.08.12
 * Time: 14:49
 * To change this template use File | Settings | File Templates.
 */
public class PointF {
    public double x, y;

    public PointF(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public PointF add(PointF other) {
        return new PointF(x + other.x, y + other.y);
    }

    public PointF subtract(PointF other) {
        return new PointF(x - other.x, y - other.y);
    }

    public double length() {
        return Math.sqrt(x * x + y * y);
    }

    public double hypot() {
        return x * x + y * y;
    }

    public PointF mul(double m) {
        return new PointF(x * m, y * m);
    }

    public PointF divide(double m) {
        return new PointF(x / m, y / m);
    }

    public double distance(PointF other) {
        return Math.sqrt(Math.pow(x - other.x, 2) + Math.pow(y - other.y, 2));
    }

    public PointF negate() {
        return new PointF(-x, -y);
    }

    public static boolean leftRotate(PointF a, PointF b, PointF c) {
        return (b.subtract(a)).multiply(c.subtract(b)) > GeometryUtils.EPS;
    }

    public static boolean rightRotate(PointF a, PointF b, PointF c) {
        return (b.subtract(a)).multiply(c.subtract(b)) < -GeometryUtils.EPS;
    }

    public static boolean collinear(PointF a, PointF b, PointF c) {
        return Math.abs(b.subtract(a).multiply(c.subtract(b))) < GeometryUtils.EPS;
    }

    public double multiply(PointF other) {
        return GeometryUtils.determinant(x, y, other.x, other.y);
    }

    public PointF rotate(double angle) {
        double nx = x * Math.cos(angle) - y * Math.sin(angle);
        double ny = x * Math.sin(angle) + y * Math.cos(angle);
        return new PointF(nx, ny);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        PointF pointF = (PointF) o;

        if (Math.abs(pointF.x - x) > GeometryUtils.EPS) return false;
        if (Math.abs(pointF.y - y) > GeometryUtils.EPS) return false;
        return true;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        temp = x != +0.0d ? Double.doubleToLongBits(x) : 0L;
        result = (int) (temp ^ (temp >>> 32));
        temp = y != +0.0d ? Double.doubleToLongBits(y) : 0L;
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    public String toString() {
        return "{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}
